<?php
declare(strict_types=1);
namespace SixShop\Payment;

use SixShop\Payment\Contracts\PaymentProviderInterface;
use SixShop\Payment\Contracts\PaymentQueryResult;
use SixShop\Payment\Contracts\PaymentRefundRequest;
use SixShop\Payment\Contracts\PaymentRefundResult;
use SixShop\Payment\Contracts\PaymentResponse;
use SixShop\Payment\Entity\ExtensionPaymentEntity;
use SixShop\Payment\Entity\ExtensionRefundEntity;
use SixShop\Payment\Enum\PaymentBizEnum;
use SixShop\Payment\Event\BeforePayEvent;
use SixShop\Payment\Event\GatheringPaymentEvent;
use SixShop\System\ExtensionManager;
use SixShop\System\Model\ExtensionModel;
use think\facade\Event;

readonly class PaymentManager
{

    public function __construct(
        private ExtensionManager $extensionManager,
    )
    {
    }

    /**
     * 获取所有支付方式
     *
     * @return PaymentInfo[]
     */
    public function getAllPayment():array
    {
        /* @var PaymentInfo[] $paymentList */
        $paymentList = Event::trigger(GatheringPaymentEvent::class);
        $paymentIDs = array_column($paymentList, 'id');
        $statusMap = ExtensionModel::whereIn('id', $paymentIDs)->column(['status','id'], 'id', true);
        $payTypeList = extension_config('payment', 'pay_type')??[];
        foreach ($paymentList as $payment) {
            $payment->status = $statusMap[$payment->id]['status'];
            $payment->enabled = in_array($payment->id, $payTypeList, true);
        }
        return $paymentList;
    }

    /**
     *  创建支付订单
     */
    public function create(string $paymentID,  array $order, PaymentBizEnum $bizType = PaymentBizEnum::ORDER_PAY):  PaymentResponse
    {
        Event::trigger(new BeforePayEvent($order, $paymentID, $bizType));
        return $this->getPaymentProvider($paymentID)->create($order, $bizType);
    }
    
    /**
     * 查询支付订单
     */
    public function query(string $paymentID, $recordID): PaymentQueryResult
    {
        return $this->getPaymentProvider($paymentID)->query($recordID);
    }

    /**
     * 退款
     */
    public function refund(string $paymentID, int $recordID, PaymentRefundRequest $param): PaymentRefundResult
    {
        return $this->getPaymentProvider($paymentID)->refund($recordID, $param);
    }
    
    /**
     * 查询退款
     */
    public function refundQuery(string $paymentID, int $refundID): PaymentRefundResult
    {
        return $this->getPaymentProvider($paymentID)->refundQuery($refundID);
    }
    
    /**
     * 获取指定支付方式
     */
    public function getPayment($paymentID):array
    {
        return [
            // todo
        ];
    }

    /**
     * 开启支付方式
     */
    public function enablePayment($paymentID):bool
    {
        // todo
        return true;
    }

    /**
     * 关闭支付方式
     */
    public function disablePayment($paymentID):bool
    {
        // todo
        return true;
    }

    public function getPaymentProvider(string $paymentID): PaymentProviderInterface
    {
        $extension = $this->extensionManager->getExtension($paymentID);
        return $extension->getPaymentProvider();
    }

}